package pjn;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.aliasi.util.Files;

public class Main {
    private File inputFile;
    private Splitter splitter = new Splitter();
    private CharactersFinder charactersFinder = new CharactersFinder();
    private GramaticalResolver gramaticalResolver = new GramaticalResolver();
    private CharactersEvaluator charactersEvaluator = new CharactersEvaluator();

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Prosze podac sciezke do pliku tekstowego ksiazki");
            return;
        } else if (!new File(args[0]).exists()) {
            System.out.println("Nie ma takiego pliku");
            return;
        }

        new Main(args[0]).execute();
    }

    public Main(String inputFileName) {
        this.inputFile = new File(inputFileName);
        JCLP.init();
    }

    public void execute() {
        try {
            String text = Files.readFromFile(inputFile, "ISO-8859-2");

            List<String> sentences = splitter.splitIntoSentences(text);
            Map<Integer, Set<Integer>> charactersInSentencesCount = new HashMap<Integer, Set<Integer>>(); //klucz - liczba bohaterow, wartosc - zbior numerow zdan
            Map<Character, Map<Character, MultiWordCharacterHelper>> charactersPairs = new HashMap<Character, Map<Character, MultiWordCharacterHelper>>();

            Set<Character> characters = charactersFinder.findCharacters(sentences, charactersInSentencesCount, charactersPairs);
            gramaticalResolver.resolveCharactersGendersAndNumbers(characters, sentences, charactersInSentencesCount);
            charactersFinder.findMultiWordCharacters(characters, charactersPairs);
            charactersEvaluator.findPositiveAndNegativeSentences(characters, sentences, charactersInSentencesCount);

            List<Character> characterList = new ArrayList<Character>(characters);
            Collections.sort(characterList, new Comparator<Character>() {
                @Override
                public int compare(Character character1, Character character2) {
                    return character2.getTotalOccurrenceCount().compareTo(character1.getTotalOccurrenceCount());
                }

            });

            for (Character character : characterList) {
                System.out.println(character.toString());
                System.out.println("\nZdania z pozytywnymi cechami:");
                for (Integer sentenceNumber : character.getPositiveSentences()) {
                    System.out.println(sentences.get(sentenceNumber) + "\n");
                }
                System.out.println("\nZdania z negatywnymi cechami:");
                for (Integer sentenceNumber : character.getNegativeSentences()) {
                    System.out.println(sentences.get(sentenceNumber) + "\n");
                }
                System.out.println("\n");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
